<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 3px 3px 3px 3px">
                <form id="form1" class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">产品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="itemName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">产品编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="itemNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">客人货号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customerItemsNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工厂货号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="supplierItemsNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-refresh-btn"><i class="layui-icon layui-icon-refresh"></i> 刷新
                            </button>
                            <button type="button" class="layui-btn layui-btn-normal"
                                    id="importItem"> 导入产品
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="view"> 查看</button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="uploadFile"> 上传产品图片
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="createQuotation"> 生成报价单
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="createSample"> 生成样品单
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="export"> 导出</button>
                limit: 5,
                <button type="submit" class="layui-btn  layui-btn-sm layui-btn-sm data-add-btn"
                        lay-event="importItemDemo">
                    下载导入模板
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete"> 删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
        </script>

    </div>
</div>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    let user = $.common.getUserInfo();
    let isUser = $.common.checkUserAdmin();
    layui.use(['form', 'table', 'upload', 'laydate'], function () {
        var lay, form, table, upload, laydate;
        lay = layui.jquery;
        form = layui.form;
        table = layui.table;
        upload = layui.upload;
        laydate = layui.laydate;
        //初始化时间
        laydate.render({
            elem: '#createTimeStart'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#createTimeEnd'
            , type: 'datetime'
        });
        table.render({
            elem: '#currentTableId',
            url: '/item/pages',
            toolbar: '#toolbarDemo',
            method: "post",
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            contentType: "application/json; charset=utf-8",
            where: {
                param: {}
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 65, title: 'ID', sort: true},
                {field: 'itemName', width: 150, title: '产品名称'},
                {field: 'itemNo', width: 100, title: '产品编号'},
                {field: 'itemUnit', width: 100, title: '产品单位'},
                {field: 'itemWeight', width: 100, title: '产品重量'},
                {field: 'itemQuality', width: 100, title: '材质分析'},
                {field: 'customerItemsNo', width: 100, title: '客人货号'},
                {field: 'supplierItemsNo', width: 100, title: '工厂货号'},
                {
                    field: 'isTax', width: 100, title: '是否含税', templet: function (a) {
                        if (a.isTax == "yes") {
                            return "是";
                        }
                        if (a.isTax == "no") {
                            return "否";
                        }
                        return a.isTax;
                    }
                },
                {field: 'supplierPrice', width: 100, title: '"工厂价格'},
                {
                    field: 'itemSizeLength', width: 160, title: '产品尺寸-长', templet: function (a) {
                        if (a.itemSizeLength) {
                            return $.utils.lengthConversion(a.itemSizeLength);
                        }
                        return '';
                    }
                },
                {
                    field: 'itemSizeWidth', width: 160, title: '产品尺寸-宽', templet: function (a) {
                        if (a.itemSizeWidth) {
                            return $.utils.lengthConversion(a.itemSizeWidth);
                        }
                        return '';
                    }
                },
                {
                    field: 'itemSizeHeight', width: 160, title: '产品尺寸-高', templet: function (a) {
                        if (a.itemSizeHeight) {
                            return $.utils.lengthConversion(a.itemSizeHeight);
                        }
                        return '';
                    }
                },
                {field: 'innerBoxSizeLength', width: 120, title: '内箱-长cm'},
                {field: 'innerBoxSizeWidth', width: 120, title: '内箱-宽cm'},
                {field: 'innerBoxSizeHeight', width: 120, title: '内箱-高cm'},
                {field: 'boxSizeLength', width: 120, title: '外箱-长cm'},
                {field: 'boxSizeWidth', width: 120, title: '外箱-宽'},
                {field: 'boxSizeHeight', width: 120, title: '外箱-高'},
                {field: 'boxSroughWeight', width: 100, title: '外箱-毛重'},
                {field: 'boxSuttle', width: 100, title: '外箱-净重'},
                {field: 'cubeNumber', width: 100, title: '立方数'},
                {field: 'justCount', width: 100, title: '才数'},
                {field: 'innerBoxNumber', width: 80, title: '內盒数量'},
                {field: 'boxNumber', width: 120, title: '外箱数量'},
                {field: 'supplierId', width: 80, title: '工厂ID'},
                {field: 'supplierNo', width: 120, title: '工厂编号'},
                {field: 'supplierName', width: 120, title: '工厂名称'},
                {
                    field: 'createTime',
                    width: 120,
                    title: '创建时间',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                },
                {field: 'updateUser', width: 100, title: '修改人'},
                {
                    field: 'updateTime',
                    width: 120,
                    title: '修改时间',
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd')}}</div>"
                },
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
            ]],
            done: function (res, curr, count) {  //隐藏列
                if ($.common.userIsAdmin()) {
                    $("[data-field='updateUser']").css('display', 'none');
                    $("[data-field='updateTime']").css('display', 'none');
                }
                if(!$.common.userIsShowPower()){
                    $("[data-field='supplierPrice']").css('display', 'none');
                }
            },
            limits: [5, 10, 15, 20, 25, 50, 100, 500],
            limit: 10,
            page: true
        });

        var uploadInst = upload.render({
            elem: '#importItem' //绑定元素
            , url: '/item/importItem' //上传接口
            , accept: 'file'
            , exts: 'xls|xlsx'//限制上传文件的后缀名，用|分隔
            , done: function (res) {
                if (res.code == 0) {
                    $.common.showSuccessMessage(res.message)
                    search(null, table);
                } else {
                    $.common.showErrorMessage(res.message);
                    return false;
                }
            }
            , error: function () {
                //请求异常回调
                $.common.showErrorMessage("请求异常");
                return false;
            }
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            search(data, table);
            return false;
        });

        form.on('submit(data-refresh-btn)', function (data) {
            // 提交刷新了当前页面
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加产品',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/item/add.html',
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'uploadFile') {
                var index = layer.open({
                    title: '上传产品图片',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '80%'],
                    content: '/static/page/item/uploadFile.html',
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            }else if (obj.event === 'createQuotation') {
                let selectData = table.checkStatus('currentTableId');
                if (selectData.data.length < 1) {
                    $.common.showErrorMessage("请至少选择一条数据");
                    return false;
                }
                let ids = $.utils.getIds(selectData.data);
                ids=encodeURIComponent(JSON.stringify(ids));
                var index = layer.open({
                    title: '生成报价单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/item/createQuotation.html?ids='+ids,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            }else if (obj.event === 'createSample') {
                let selectData = table.checkStatus('currentTableId');
                if (selectData.data.length < 1) {
                    $.common.showErrorMessage("请至少选择一条数据");
                    return false;
                }
                let ids = $.utils.getIds(selectData.data);
                ids=encodeURIComponent(JSON.stringify(ids));
                var index = layer.open({
                    title: '生成样品单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/item/createSample.html?ids='+ids,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'export') {  //导出
                var d = {};
                //转换成JSON数据
                var t = $("#form1").serializeArray();
                $.each(t, function () {
                    d[this.name] = this.value;
                });
                let selectData = table.checkStatus('currentTableId');
                if (selectData.data.length > 0) {
                    d['idBatch'] = $.utils.getIds(selectData.data);
                }
                // let  options={};
                // options.url="/item/export";
                // options.method='post';
                // options.data=d;
                url = '/item/export/' + encodeURIComponent(JSON.stringify(d));
                window.location.href = url;

                // let selectData = table.checkStatus('currentTableId');
                // if (selectData.data.length > 0) {
                //    d.idBatch=$.utils.getIds(selectData.data);
                // }
                // // let  options={};
                // // options.url="/item/export";
                // // options.method='post';
                // // options.data=d;
                // url = '/item/export/' + JSON.stringify(d)
                // console.log(url);
                // window.location.href = encodeURIComponent(url);
                // return;
            } else if (obj.event === 'delete') {
                let selectData = table.checkStatus('currentTableId');
                if (selectData.data.length < 1) {
                    $.common.showErrorMessage("请至少选择一条数据");
                    return false;
                }
                let ids = $.utils.getIds(selectData.data);
                layer.confirm('真的删除选择的数据吗', function (index) {
                    let url = "/item/deleteByIds";
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                layer.close(index);
                                $.common.showSuccessMessage(result.message);
                                search(null, table);
                                return false;
                            } else {
                                $.common.showErrorMessage(result.message)
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(ids), callback);
                });
            } else if (obj.event === 'importItemDemo') {
                window.location.href = '/static/template/item_import.xlsx';
            } else if (obj.event === 'view') {
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let data = checkStatus.data[0];
                var index = layer.open({
                    title: '查看产品',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['90%', '95%'],
                    content: '/static/page/item/view.html',
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            body.find("#" + key).val(data[key]);
                            //图片处理
                            if ('photo' == key && data[key]) {
                                let array = data[key].split(";");
                                for (let data in array) {
                                    let ph = array[data]
                                    if (ph) {
                                        body.find('#uploadImg').append('<img src="' + ph + '" class="layui-upload-img" width="50%" height="50%">');
                                    }
                                }
                            }
                        }
                        body.find("input").attr("disabled", true);
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '修改产品',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['90%', '95%'],
                    content: '/static/page/item/add.html?supplierId=' + data.supplierId + "&isTax=" + data.isTax,
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            body.find("#" + key).val(data[key]);
                            //图片处理
                            if ('photo' == key && data[key]) {
                                let array = data[key].split(";");
                                for (let data in array) {
                                    let ph = array[data]
                                    if (ph) {
                                        body.find('#uploadImg').append('<img src="' + ph + '" class="layui-upload-img" width="50%" height="50%">');
                                    }
                                }
                            }
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除？', function (index) {
                    let url = "/item/delete/" + obj.data.id;
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                window.location.reload();
                                // obj.del();
                                // layer.close(index);
                            } else {
                                $.common.showErrorMessage(result.message)
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
                });
            }
        });

    })
    ;

    function search(data, table) {
        let param = {};
        if (data) {
            param = data.field;
        }
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                param: param
            }
        }, 'data');
    }
</script>

</body>
</html>
